﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.AdminCP.Common;
using LinkCard.Dao;
using LinkCard.Model;

namespace LinkCard.AdminCP.User
{
    public partial class user_edit : System.Web.UI.Page
    {
        public Int64 nUserID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "STAFF") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }
            if (Request.QueryString["UserID"] != null)
                nUserID = Convert.ToInt64(Request.QueryString["UserID"].ToString());
            if (!IsPostBack)
                LoadUser();
        }
        protected bool checkUsername(string sUsername)
        {
            string sSQL = "SELECT UserID FROM user WHERE Username = N'" + sUsername + "'";
            DataTable dt = DataHelper.SelectTable(sSQL, false);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                // Permission
                string sPermission1 = "0";
                string sPermission2 = "0";
                string sPermission3 = "0";
                string sPermission4 = "0";
                string sPermission5 = "0";
                string sPermission6 = "0";
                string sPermission7 = "0";

                if (Permission1.Checked) sPermission1 = "1"; // Quản lý Hệ thống
                if (Permission2.Checked) sPermission2 = "1"; // Quản lý Khách hàng
                if (Permission3.Checked) sPermission3 = "1"; // Quản lý Nhân viên
                if (Permission4.Checked) sPermission4 = "1"; // Quản lý Đối tác
                if (Permission5.Checked) sPermission5 = "1"; // Quản lý Tin bài - Trang chủ
                if (Permission6.Checked) sPermission6 = "1"; // Xem - duyệt Báo cáo
                
                string sPermission = "";
                sPermission = sPermission1 + sPermission2 + sPermission3 + sPermission4 + sPermission5 + sPermission6 + sPermission7;

                var db = new TopLinkEntities();
                var _user = db.Users.Where(t => t.UserID == nUserID).FirstOrDefault();
                
                _user.Username = Username.Value;
                if ((Password.Value.Trim() != "") && Password.Value.Trim() != oldPassword.Value.Trim())
                    _user.Password = Password.Value;

                _user.Permission = sPermission;
                _user.Description = Description.Value;
                _user.Fullname = Fullname.Value;
                _user.MobilePhone = MobilePhone.Value;
                _user.Address = Address.Value;
                _user.Email = Email.Value;
                _user.Type = "NORMAL";

                db.SaveChanges();

                Response.Redirect("/User/user_list.aspx", false);

            }
            catch (Exception ex)
            {
                Response.Redirect("/message_box.aspx?msg=6", false);
            }

        }

        private void LoadUser()
        {
            try
            {
                var db = new TopLinkEntities();
                var _user = db.Users.Where(t => t.UserID == nUserID).FirstOrDefault();

                this.Username.Value = _user.Username;
                this.oldPassword.Value = _user.Password;
                this.Description.Value = _user.Description;
                this.Fullname.Value = _user.Fullname;
                this.MobilePhone.Value = _user.MobilePhone;
                this.Address.Value = _user.Address;
                this.Email.Value = _user.Email;
                
                //set permission
                char[] permission = _user.Permission.ToCharArray();
                if (permission[0].ToString() == "1")
                    Permission1.Checked = true;
                if (permission[1].ToString() == "1")
                    Permission2.Checked = true;
                if (permission[2].ToString() == "1")
                    Permission3.Checked = true;
                if (permission[3].ToString() == "1")
                    Permission4.Checked = true;
                if (permission[4].ToString() == "1")
                    Permission5.Checked = true;
                if (permission[5].ToString() == "1")
                    Permission6.Checked = true;
            }
            catch
            {
                Response.Redirect("/message_box.aspx?msg=6", true);
            }

        }
    }
}